<?php
/**
 * Created by PhpStorm.
 * UserModel: Administrator
 * Date: 2019/3/01/0001
 * Time: 10:41
 */
namespace app\caidongweb\model;
use think\Session;

/**
 * Class AdminModel
 * 总后台管理员管理类
 *
 * @package app\caidongweb\Model
 */
class AdminModel extends BaseModel
{
    protected $table = 'cd_admin';

    /**
     * function login
     * 总后台管理员登录
     *
     * @return array
     */
    public function login()
    {
        $data = input("post.");
        $res = $this->where(['ad_name'=>$data['username'],'ad_pwd'=>md5($data['password'])])->find();
        if($res){
            if(!captcha_check($data['code'])){
                $status = ['code'=>-1,'msg'=>'验证码不正确'];
            }else{
                if($res['ad_state']==0){
                    $status = ['code'=>-1,'msg'=>'该账号已被禁用'];
                    accountLog(3,1,$data['username'],'后台登录禁用账号');
                }else{
                    //设置session
                    Session::set('usid',$res['ad_id']);
                    Session::set('sname',$res['ad_name']);
                    Session::set('count',$res['ad_count']);
                    //记录登录操作
                    $this->where('ad_id',$res['ad_id'])->setInc('ad_count',1);
                    $loginTime = $this->where('ad_id',$res['ad_id'])->update(['ad_dtime'=>time()]);
                    if($loginTime){
                        accountLog(1,1,$data['username'],'登录后台');
                        $status = ['code'=>1,'msg'=>'登录成功'];
                    } else {
                        $status = ['code'=>-1,'msg'=>'登录失败，请重试'];
                    }
                }
            }
        }else{
            accountLog(3,1,$data['username'],'登录后台账号密码错误');
            $status = ['code'=>-1,'msg'=>'用户名或密码不正确'];
        }
        return $status;
    }

    /**
     * function getList
     * 后台角色列表
     *
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        $where = [];
        input('g_id') ? $where['g_id'] = ['=',input('g_id')]: false;
        input('username') ? $where['ad_name'] = input('username') : false;
        $list = $this->alias('a')
            ->field('*,a.ad_id as aid,a.ad_state as astatus')
            ->join('cd_auth_group g','g.id = a.g_id')
            ->where($where)
            ->paginate(30,false,['query'=>request()->param()]);
        return $list;
    }
    /**
     * function edit
     * 后台用户编辑
     *
     * @return array
     */
    public function edit(){
        $data = input('post.');
        if($data['ad_pwd'] == ''){
            unset($data['ad_pwd']);
        }else{
            $data['ad_pwd'] = md5($data['ad_pwd']);
        }
        if($data['ad_state']==''){
           return $status = $this->returnData(-1,'状态不能为空');
        }
        if($data['g_id'] == ''){
            return $status = $this->returnData(-1,'请选择用户组');
        }
        $res = $this->where('ad_id',$data['ad_id'])->update($data);
        if($res!==false){
            $status = $this->returnData(1,'修改成功');
        }else{
            $status = $this->returnData(-1,'修改失败');
        }
        return $status;
    }
    /**
     * function add
     * 后台用户添加
     *
     * @return array
     */
    public function add(){
        $data = input('post.');
        $info = $this->where('ad_name',$data['ad_name'])->find();
        if($info){
            return $status = $this->returnData(-1,'该用户已存在');
        }
        if($data['ad_pwd'] == ''){
            return $status = $this->returnData(-1,'密码不能为空');
        }else{
            $data['ad_pwd'] = md5($data['ad_pwd']);
        }
        if($data['ad_state']==''){
            return $status = $this->returnData(-1,'状态不能为空');
        }
        if($data['g_id'] == ''){
            return $status = $this->returnData(-1,'请选择用户组');
        }
        $data['ad_ztime'] = time();
        $data['ad_dtime'] = time();
        $res = $this->insert($data);
        if($res!==false){
            $status = $this->returnData(1,'添加成功');
        }else{
            $status = $this->returnData(-1,'添加失败');
        }
        return $status;
    }
    /**
     * function delList
     * 批量删除
     *
     * @return array
     */
    public function delList()
    {
        $id = input('post.id/a');
        $res = $this->deletes($this->table,$id);
        return $res;
    }
    /**
     * function getUserInfo
     * 获取用户信息
     *
     * @return array
     */
    public function getUserInfo($id = '')
    {
        $data = $this->alias('a')
            ->join('cd_auth_group g','a.g_id=g.id')
            ->where('ad_id',$id)
            ->field('a.*,g.title')
            ->find();
        return $data;
    }
    /**
     * function updateStatus
     * 编辑状态
     *
     * @return array
     */
    public function updateStatus()
    {
        $data = input('post.');
        $result = $this->where('id',$data['id'])->update(['status'=>$data['status']]);
        if($result){
            return $this->returnData(1,'编辑成功');
        }else{
            return $this->returnData(-1,'编辑失败');
        }
    }
}